package JZ;

import java.util.Arrays;

/**
 * @Classname JZ07
 * @Description // https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/ 树
 * @Date 2021/4/21 11:28
 * @Created by yin
 */
public class JZ07 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder.length == 0) return null;
        TreeNode root = new TreeNode(preorder[0]);
        for (int i = 0; i < inorder.length; i++) {
            if (preorder[0] == inorder[i]) {
                root.left = buildTree(Arrays.copyOfRange(preorder, 1, i + 1), Arrays.copyOfRange(inorder, 0, i));
                root.right = buildTree(Arrays.copyOfRange(preorder, i + 1, inorder.length), Arrays.copyOfRange(inorder, i + 1, inorder.length));
            }
        }
        return root;
    }

}
